#!/bin/sh -e

export DESTDIR="$1"

# Remove 'libelf' and 'util-linux' dependencies.
sed -i 's/HAVE_LIBELF/HAVE_KISS/'      gio/meson.build
sed -i 's/libmount_dep.found()/false/' meson.build

# Don't build tests/fuzzing stuff.
find . -type f -name meson.build -exec \
    sed -i "/subdir('tests')/d;/subdir('fuzzing')/d" {} +

meson \
    --prefix=/usr \
    -Dlibmount=disabled \
    -Dinstalled_tests=false \
    -Ddefault_library=both \
    -Dman=false \
    -Dfam=false \
    -Dinternal_pcre=true \
    . build

ninja -C build
ninja -C build install

# json-glib is a separate package which we build alongside
# glib (as if they were one and the same). The upstream for
# json-glib is effectively dead (only translation work) so
# this makes my life a little easier..
{
    cd json-glib

    # This mess of CFLAGS, etc is needed to point json-glib to
    # the freshly built glib sources instead of the system's.
    export PKG_CONFIG_PATH="$1/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
    export PATH="$1/usr/bin:$PATH"
    export CFLAGS="$CFLAGS -I$1/usr/include/glib-2.0"
    export CFLAGS="$CFLAGS -I$1/usr/lib/glib-2.0/include -pthread"
    export LDFLAGS="$LDFLAGS -L$1/usr/lib/"

    meson \
        --prefix=/usr \
        --sysconfdir=/etc \
        --mandir=/usr/share/man \
        -Ddocs=false \
        -Dintrospection=false \
        . output

    ninja -C output
    ninja -C output install
}

kiss l dbus >/dev/null || rm -rf "$1/usr/bin/gdbus"
